<?php 
header("Pragma: public");
header("Expires: 0");
header("Accept-Ranges: bytes");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.ms-word");
header('Content-Disposition: attachment; filename=facultetrating.xls');
header("Content-Transfer-Encoding: binary");
?>
<table width="100%" cellspacing="2" cellpadding="0" border=1>
  <tr>
    <th><strong>id</strong></th>
    <th><strong>ФИО</strong></th>
    <th><strong>Выбранные специальности</strong></th>
    <th><strong>Оригинал документов</strong></th>
    <th><strong>Баллы по предметам</strong></th>   
    <th><strong>Баллы всего </strong></th>        
    <th><strong>Контактные данные</strong></th>
    <th><strong>Родственники Контактные данные</strong></th>
    <th><strong>Данные целевого</strong></th>
  </tr>
  <?php 
  foreach ($all_abiturients as $profession_abiturients):
  	echo '<tr colspan=7><td><strong>'.$professions[$profession_abiturients['profession_id']].'</strong></td></tr>';
    $sorted_abiturients = $profession_abiturients['sorted_score_abiturients_ids'];
  	while (list($key, $val) = each($sorted_abiturients)) {
  		echo '<tr>';
  			$abiturient_id = $key;
  			foreach ($all_abiturientsinfo as $abiturient):
				if ($abiturient['Abiturient']['id'] == $abiturient_id){
					break;
				}
			endforeach;	
				echo '<td>';
				echo $abiturient['Abiturient']['id'];
				echo '</td>';
				echo '<td>';
				echo $abiturient['Abiturient']['lastname'].' ';
				echo $abiturient['Abiturient']['firstname'].' ';
				echo $abiturient['Abiturient']['surname'];
				echo '</td>';				
				echo '<td>';
				foreach ($abiturient['Selectedprofession'] as $selectedprofession):
					echo $professions[$selectedprofession['profession_id']].' '.$learningforms[$selectedprofession['learningform_id']].' '.$learningtypes[$selectedprofession['learningtype_id']].'<br>';
				endforeach;
				echo '<td>';
				if ($abiturient['Abiturient']['originaleducationdoc']){
					echo '<font color=green>ДА</font>';
				} else {
					echo '<font color=red>НЕТ</font>';
				} 
// баллы по предметам
				echo '<td>';
				$total_score = 0;
					foreach ($professions_subjects as $profession_subjects):
						if ($profession_subjects['profession_id'] == $profession_abiturients['profession_id']) {
							$subjects_ids = $profession_subjects['subject_ids'];
							break;
						}
					endforeach;
				
				foreach ($subjects_ids as $subject_id):
					echo $subjects[$subject_id];
					if ($abiturient['Result'] == NULL){
						echo '<br />';
					}
					foreach ($abiturient['Result'] as $result):
						if ($result['resulttype_id'] == 1){ // 1 ЕГЕ 2 ФЦТ 3 Олимпиада
							if ($subject_id == $result['subject_id']){
								$total_score += $result['score'];
								if ($result['score'] == $result['fbasescore']) {
									echo ' - <font color=green>'.$result['score'].'</font><br />';
								} else {
									echo ' - <font color=red>'.$result['score'].'</font><br />';
								}
							}
						} else {
							if ($subject_id == $result['subject_id']){
								$total_score += $result['score'];					
								echo ' - '.$result['score'].'<br />';
							}	
						}	
					endforeach;
				endforeach;
				echo '<font color="green">Всего - '.$total_score.'</font>';
				echo '</td>';						
				echo '<td align=center>';
				echo $val;				
				echo '</td>';
// телефоны абитуриета				
				echo '<td>';
				echo 'тел '.$abiturient['Abiturient']['phone'].'<br>';
				echo 'моб '.$abiturient['Abiturient']['mobile'].'<br>';
				echo '</td>';	
				// вывод данных родственников				
				echo '<td>';
				$relatives = $abiturient['Relative'];
				foreach ($relatives as $relative):
					echo $relative['fio'].'<br>';
				echo 'тел '.$relative['phone'].'<br>';
				echo 'моб '.$relative['mobile'].'<br>';				
				endforeach;
				echo '</td>';
// данные о целевом направлении	
				echo '<td>';
				echo 'область - <b>'.$regions[$abiturient['Targetingintake'][0]['region_id']].'</b><br>';
				echo $tgintakeorgs[$abiturient['Targetingintake'][0]['tgintakeorg_id']].'<br>';
				echo '</td>';
  		echo '</tr>';
  	}
  endforeach;
  
  
  
  
  ?>
 </table> 
 <?php debug ($all_abiturientsinfo);?>